using System;
using System.Collections.Generic;
using ICSharpCode.TextEditor.Document;

namespace ICSharpCode.TextEditor.UserControls
{
	public class HighlightGroup : IDisposable
	{
		private readonly List<TextMarker> _markers = new List<TextMarker>();

		private readonly TextEditorControl _editor;

		private readonly IDocument _document;

		public IList<TextMarker> Markers => this._markers.AsReadOnly();

		public HighlightGroup(TextEditorControl editor)
		{
			this._editor = editor;
			this._document = editor.Document;
		}

		public void AddMarker(TextMarker marker)
		{
			this._markers.Add(marker);
			this._document.MarkerStrategy.AddMarker(marker);
		}

		public void ClearMarkers()
		{
			foreach (TextMarker marker in this._markers)
			{
				this._document.MarkerStrategy.RemoveMarker(marker);
			}
			this._markers.Clear();
			this._editor.Refresh();
		}

		public void Dispose()
		{
			this.ClearMarkers();
			GC.SuppressFinalize(this);
		}

		~HighlightGroup()
		{
			this.Dispose();
		}
	}
}
